package com.lincoln.bean;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Getter
@Setter
@AllArgsConstructor
public class ApiCodeException extends Exception {
    protected static final Logger logger = LoggerFactory.getLogger(ApiCodeException.class);
    ApiCode apiCode;
    public ApiCodeException(){
        this.apiCode = ApiCode.findCode(ApiCode.FAIL);
        log();
    }

    private void log(){
        logger.error(apiCode.getMsg());
    }

    public ApiCodeException(int apicode){
        this.apiCode = ApiCode.findCode(apicode);
        if(apiCode == null){
            this.apiCode = ApiCode.findCode(ApiCode.FAIL);
        }
        log();
    }

    public ApiCodeException(int apicode,String msg){
        this.apiCode = ApiCode.findCode(apicode);
        if(apiCode == null){
            this.apiCode = ApiCode.findCode(ApiCode.FAIL);
        }
        this.apiCode.setMsg(msg);
        log();
    }

    public ApiCode getApiCode(){
        return apiCode;
    }

    public String toString(){
        return apiCode.getMsg();
    }
}
